{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "provenance": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "source": [
        "<a href=\"https://colab.research.google.com/drive/1l7B3kaS7BswwbIu4TBrLTX_uqraZjqho?usp=sharing\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"></a>\n",
        "\n",
        "### [Calude](https://www.anthropic.com/claude/) - Anthropic's Claude based LLM API call.\n"
      ],
      "metadata": {
        "id": "HxxOWz9dpsYj"
      }
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Install required libraries"
      ],
      "metadata": {
        "id": "Y3axTI0sp5Hg"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "!pip install -q -U langchain-anthropic==0.3.7"
      ],
      "metadata": {
        "id": "ShxTNxM5gqtr",
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "outputId": "e0436b32-cf09-40ee-ab6d-577022f18576"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "\u001b[?25l   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/223.2 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K   \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━\u001b[0m \u001b[32m215.0/223.2 kB\u001b[0m \u001b[31m14.3 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K   \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m223.2/223.2 kB\u001b[0m \u001b[31m5.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
            "\u001b[?25h"
          ]
        }
      ]
    },
    {
      "cell_type": "markdown",
      "source": [
        "### Import related libraries"
      ],
      "metadata": {
        "id": "9jJ1vqs-p_Zx"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "from langchain_anthropic import ChatAnthropic"
      ],
      "metadata": {
        "id": "RL-3LsYogoH5"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "import getpass\n",
        "import os"
      ],
      "metadata": {
        "id": "GT55z5AkhyOW"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "source": [
        "#### Provide a Anthropic (Claude) API key (Paid one)\n",
        "\n",
        "[Anthropic API Key Creation Link](https://console.anthropic.com/settings/keys)\n",
        "\n",
        "\n"
      ],
      "metadata": {
        "id": "F6UeDlrgqI2A"
      }
    },
    {
      "cell_type": "code",
      "source": [
        "os.environ[\"ANTHROPIC_API_KEY\"] = getpass.getpass()"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "yobvrD3glfd4",
        "outputId": "a83be4ed-3ef1-41bc-eeee-8ee0e6a6591d"
      },
      "execution_count": null,
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "··········\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [
        "llm = ChatAnthropic(\n",
        "    model=\"claude-3-5-sonnet-20241022\",\n",
        "    temperature=0.5\n",
        ")\n",
        "\n",
        "messages = [\n",
        "    (\n",
        "        \"system\",\n",
        "        \"You are a helpful assistant that explains problems step-by-step.\",\n",
        "    ),\n",
        "    (\"human\", \"Solve this problem step by step: {problem}\"),\n",
        "]"
      ],
      "metadata": {
        "id": "HHXlnTcxpi-8"
      },
      "execution_count": null,
      "outputs": []
    },
    {
      "cell_type": "code",
      "source": [
        "ai_msg = llm.invoke(\"Provide me python code of sudoku\")\n",
        "print(ai_msg.content)"
      ],
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "f4OZQ-kvqNl5",
        "outputId": "d437e20d-5b4c-4feb-abfe-1e54b05845f4"
      },
      "execution_count": null,
      "outputs": [
        {
          "output_type": "stream",
          "name": "stdout",
          "text": [
            "Here's a Python implementation of a Sudoku solver using a backtracking algorithm:\n",
            "\n",
            "```python\n",
            "def print_board(board):\n",
            "    for i in range(len(board)):\n",
            "        if i % 3 == 0 and i != 0:\n",
            "            print(\"- - - - - - - - - - - -\")\n",
            "        \n",
            "        for j in range(len(board[0])):\n",
            "            if j % 3 == 0 and j != 0:\n",
            "                print(\"|\", end=\" \")\n",
            "            \n",
            "            if j == 8:\n",
            "                print(board[i][j])\n",
            "            else:\n",
            "                print(str(board[i][j]) + \" \", end=\"\")\n",
            "\n",
            "def find_empty(board):\n",
            "    for i in range(len(board)):\n",
            "        for j in range(len(board[0])):\n",
            "            if board[i][j] == 0:\n",
            "                return (i, j)  # row, col\n",
            "    return None\n",
            "\n",
            "def valid(board, num, pos):\n",
            "    # Check row\n",
            "    for j in range(len(board[0])):\n",
            "        if board[pos[0]][j] == num and pos[1] != j:\n",
            "            return False\n",
            "    \n",
            "    # Check column\n",
            "    for i in range(len(board)):\n",
            "        if board[i][pos[1]] == num and pos[0] != i:\n",
            "            return False\n",
            "    \n",
            "    # Check box\n",
            "    box_x = pos[1] // 3\n",
            "    box_y = pos[0] // 3\n",
            "\n",
            "    for i in range(box_y * 3, box_y * 3 + 3):\n",
            "        for j in range(box_x * 3, box_x * 3 + 3):\n",
            "            if board[i][j] == num and (i, j) != pos:\n",
            "                return False\n",
            "    \n",
            "    return True\n",
            "\n",
            "def solve(board):\n",
            "    empty = find_empty(board)\n",
            "    if not empty:\n",
            "        return True\n",
            "    \n",
            "    row, col = empty\n",
            "\n",
            "    for num in range(1, 10):\n",
            "        if valid(board, num, (row, col)):\n",
            "            board[row][col] = num\n",
            "\n",
            "            if solve(board):\n",
            "                return True\n",
            "            \n",
            "            board[row][col] = 0\n",
            "    \n",
            "    return False\n",
            "\n",
            "# Example Sudoku board (0 represents empty cells)\n",
            "board = [\n",
            "    [5, 3, 0, 0, 7, 0, 0, 0, 0],\n",
            "    [6, 0, 0, 1, 9, 5, 0, 0, 0],\n",
            "    [0, 9, 8, 0, 0, 0, 0, 6, 0],\n",
            "    [8, 0, 0, 0, 6, 0, 0, 0, 3],\n",
            "    [4, 0, 0, 8, 0, 3, 0, 0, 1],\n",
            "    [7, 0, 0, 0, 2, 0, 0, 0, 6],\n",
            "    [0, 6, 0, 0, 0, 0, 2, 8, 0],\n",
            "    [0, 0, 0, 4, 1, 9, 0, 0, 5],\n",
            "    [0, 0, 0, 0, 8, 0, 0, 7, 9]\n",
            "]\n",
            "\n",
            "print(\"Original Sudoku board:\")\n",
            "print_board(board)\n",
            "print(\"\\nSolving...\\n\")\n",
            "\n",
            "if solve(board):\n",
            "    print(\"Solved Sudoku board:\")\n",
            "    print_board(board)\n",
            "else:\n",
            "    print(\"No solution exists\")\n",
            "```\n",
            "\n",
            "This code includes:\n",
            "\n",
            "1. `print_board(board)`: Function to print the Sudoku board in a readable format\n",
            "\n",
            "2. `find_empty(board)`: Function to find an empty cell (represented by 0)\n",
            "\n",
            "3. `valid(board, num, pos)`: Function to check if a number is valid in a given position\n",
            "\n",
            "4. `solve(board)`: Main solving function using backtracking algorithm\n",
            "\n",
            "The program works as follows:\n",
            "\n",
            "1. It starts with an incomplete Sudoku board where 0 represents empty cells\n",
            "2. The solving algorithm uses backtracking to try different numbers in empty cells\n",
            "3. For each empty cell,\n"
          ]
        }
      ]
    },
    {
      "cell_type": "code",
      "source": [],
      "metadata": {
        "id": "6S4n9GOir1Us"
      },
      "execution_count": null,
      "outputs": []
    }
  ]
}